package drops.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import drops.dao.DentalTransaction;
import drops.dao.DentalTransactionPeer;
import drops.dao.Patient;
import drops.dao.PatientPeer;

public class EditTransactionPanel extends JPanel implements  ActionListener, PropertyChangeListener {
	JLabel patientLabel;
	JLabel dateLabel;
	JLabel descriptionLabel;
	JLabel timeLabel;
	JLabel debitLabel;
	JLabel creditDateLabel;
	JLabel creditAmountLabel;
	JLabel balanceLabel;

	JFormattedTextField dateField;
	JTextField descriptionField;
	JFormattedTextField timeField;
	JFormattedTextField debitField;
	JFormattedTextField creditDateField;
	JFormattedTextField creditAmountField;
	JFormattedTextField balanceField;

	JButton submitButton;
	JButton clearButton;

	MainFrame mainFrame;
	
	JPanel header;
	JPanel body;
	JPanel footer;
	
	int transactionNumber;
	public EditTransactionPanel (MainFrame mainFrame) {
    	this.mainFrame = mainFrame;
    
    	patientLabel = new JLabel("Patient");
    	header = new JPanel();
    	header.add(patientLabel);
    	
    	dateLabel = new JLabel("Date:");
		descriptionLabel = new JLabel("Description:");
		timeLabel = new JLabel("Time:");
		debitLabel = new JLabel("Debit:");
	 	creditDateLabel = new JLabel("Credit Date:");
	 	creditAmountLabel = new JLabel("Credit Amount:");
	 	balanceLabel = new JLabel("Balance:");
	 	
	 	DateFormat df = DateFormat.getDateInstance();
    	DateFormat tf = DateFormat.getTimeInstance();
    	NumberFormat cf = NumberFormat.getNumberInstance();
	 	dateField = new JFormattedTextField(df);
    	dateField.setText(df.format(new Date()));
    	
    	descriptionField = new JTextField(50);
    	timeField = new JFormattedTextField(tf);
    	timeField.setValue(new Date());
    	debitField = new JFormattedTextField(cf);
    	debitField.setValue(0.0);
    	creditDateField = new JFormattedTextField(df);
    	creditDateField.setValue(new Date());
    	creditAmountField = new JFormattedTextField(cf);
    	creditAmountField.setValue(0.0);
    	balanceField = new JFormattedTextField(cf);
    	balanceField.setValue(0.0);
    	
    	body = new JPanel();
    	body.setLayout(new GridLayout(9,2));
    	
    	body.add(dateLabel);
    	body.add(dateField);
    	body.add(descriptionLabel);
    	body.add(descriptionField);
    	body.add(timeLabel);
    	body.add(timeField);
    	body.add(debitLabel);
    	body.add(debitField);
    	body.add(creditDateLabel);
    	body.add(creditDateField);
    	body.add(creditAmountLabel);
    	body.add(creditAmountField);
    	body.add(balanceLabel);
    	body.add(balanceField);

    	submitButton = new JButton("Submit");
    	submitButton.addActionListener(this);
    	clearButton = new JButton("Clear");
    	
    	footer = new JPanel();
    	footer.add(submitButton);
    	
    	setLayout(new BorderLayout());
    	add(header, BorderLayout.NORTH);
    	add(body, BorderLayout.CENTER);
    	add(footer, BorderLayout.SOUTH);
    }
    
    public void setTransactionNumber(int transactionNumber) {
    	this.transactionNumber = transactionNumber;
    	DentalTransaction t = DentalTransactionPeer.searchTransactionByTransactionNumber(transactionNumber);
    	dateField.setValue(t.getDate());
    	descriptionField.setText(t.getDescription());
    	timeField.setValue(t.getDate());
    	debitField.setValue(t.getDebit());
    	creditDateField.setValue(t.getCreditDate());
    	creditAmountField.setValue(t.getCreditAmount());
    	balanceField.setValue(t.getBalance());
    	Patient p = PatientPeer.searchPatientByNumber(t.getPatientNumber());
    	patientLabel.setText(p.getPatientLastName() +", " + p.getPatientFirstName() + " " + p.getPatientMiddleName().charAt(0) + ".");
    }
    
    public void editTransaction() {
		Date date = (Date)dateField.getValue();
		Date time = (Date)timeField.getValue();
		Calendar c = Calendar.getInstance();
		Calendar c2 = new GregorianCalendar();
		c.setTime(date);
		c2.setTime(time);
		c.set(Calendar.HOUR_OF_DAY, c2.get(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, c2.get(Calendar.MINUTE));
		c.set(Calendar.SECOND, c2.get(Calendar.SECOND));
		date = c.getTime();
		String description = descriptionField.getText();
		double debit = Double.parseDouble(debitField.getValue().toString());
		Date creditDate = (Date) creditDateField.getValue();
		double creditAmount = Double.parseDouble(creditAmountField.getValue().toString());
		double balance = Double.parseDouble(balanceField.getValue().toString());
		if (DentalTransactionPeer.editDentalTransaction(transactionNumber, date, description, debit, creditDate, creditAmount, balance)) {
			JOptionPane.showMessageDialog(this, "Edit Transaction Successful.");
			mainFrame.refreshTransactions();
			mainFrame.closeEditTransactionFrame();
		}
		else {
			JOptionPane.showMessageDialog(this, "Edit Transaction failed.");
		}
    }
    
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==submitButton) {
			editTransaction();
		}
	}

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
